package sk.stuba.fiit.foo07.genex.gui;

import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import sk.stuba.fiit.foo07.genex.beans.Answer;
import sk.stuba.fiit.foo07.genex.beans.Question;
import sk.stuba.fiit.foo07.genex.dao.AnswerDao;
import sk.stuba.fiit.foo07.genex.dao.AnswerDaoDerby;

public class CheckBoxTableModel extends AbstractTableModel {

    /**
     * 
     */
    private static final long serialVersionUID = -7402691643396712897L;

    private String[] columnNames = new String[2];
    private boolean singleSelect = true;
    private AnswerDao aDao;
    private ArrayList<Answer> answers;

    public CheckBoxTableModel(String t, String b) {
        setColumnNames(t, b);
        answers = new ArrayList<Answer>();
    }

    public CheckBoxTableModel(String t, String b, boolean singleSelect) {
        this(t, b);
        this.singleSelect = singleSelect;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return answers.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Answer a = answers.get(rowIndex);

        return columnIndex == 0 ? a.getText() : a.getIsCorrect();
    }

    @Override
    public void setValueAt(Object value, int r, int c) {
        Answer a = answers.get(r);

        if (c == 0) {
            a.setText((String) value);
        } else {
            if (singleSelect && ((Boolean) value).booleanValue() == false)
                return;

            if (singleSelect) {
                for (Answer tmp : answers)
                    tmp.setIsCorrect(false);
            }
            a.setIsCorrect((Boolean) value);
        }

        fireTableDataChanged();
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        return columnIndex == 0 ? String.class : Boolean.class;
    }

    @Override
    public boolean isCellEditable(int r, int c) {
        return true;
    }

    @Override
    public String getColumnName(int c) {
        return columnNames[c];
    }

    public void setColumnNames(String tColumn, String bColumn) {
        columnNames[0] = new String(tColumn);
        columnNames[1] = new String(bColumn);

        this.fireTableStructureChanged();
    }

    public void addAnswer(Answer toAdd) {
        answers.add(toAdd);
        fireTableDataChanged();
    }

    public Answer getAnswer(int r) {
        return answers.get(r);
    }

    public void removeAnswer(int r) {
        answers.remove(r);
        fireTableDataChanged();
    }

    public ArrayList<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(ArrayList<Answer> answers) {
        if (answers == null)
            this.answers.clear();
        else
            this.answers = answers;
        fireTableDataChanged();
    }

    public void setAnswers(Question q, Connection c) {
        if (q.getQuestionID() == null)
            answers.clear();
        else {
            aDao = new AnswerDaoDerby(c);
            //TODO 
            //pridane odchytavanie vynimky (palo)
            try {
                answers = aDao.getAnswersByQuestionID(q.getQuestionID());
            } catch (Exception e) {
            }
        }
        fireTableDataChanged();
    }

    public void setSingleSelect(boolean singleSelect) {
        // musime upravit data aby bol true iba jeden stlpec
        if (!this.singleSelect && singleSelect) {
            boolean first = true;
            for (Answer bs : answers) {
                if (first)
                    first = false;
                else
                    bs.setIsCorrect(false);
            }
        }

        this.singleSelect = singleSelect;

    }

    public boolean isSingleSelect() {
        return singleSelect;
    }

    public void saveAnswers(Question q, Connection c) {
        // predpoklada sa , ze otazka je v DB
        //TODO 
        //pridane odchytavanie vynimky (palo)
        try {
            aDao = new AnswerDaoDerby(c);
            ArrayList<Answer> oldAnswers = aDao.getAnswersByQuestionID(q
                    .getQuestionID());
            for (Answer a : oldAnswers)
                aDao.deleteAnswer(a);

            aDao.addAnswersToQuestion(q.getQuestionID(), answers);
        } catch (Exception e) {
        }
    }
}
